home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / ntwhich.zip / NTWHICH.C < prev    next >
C/C++ Source or Header  |  1994-07-24  |  6KB  |  235 lines

  1. /* ntwhich.c ... find first matching executable in path,
  2.  * be it .COM .BAT .EXE or .CMD
  3.  * Copyright (C) 1994 by
  4.  * jack j. woehr, p.o. box 51, golden, colorado 80402-0051
  5.  * jax@well.sf.ca.us JAX on GEnie 72203.1320@compuserve.com
  6.  * SYSOP, RealTime Control & Forth Board [RCFB] (303) 278-0364
  7.  * All Rights Reserved
  8.  */
  9.  
  10. /*
  11.  * This is free software and can be modified and redistributed under
  12.  * certain conditions described in the file COPYING.TXT. The
  13.  * Disclaimer of Warranty and License for this free software are also
  14.  * contained in the file COPYING.TXT.
  15.  */
  16.  
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include "ntwhich.h"
  20.  
  21. const char extensions[4][5] = { ".COM", ".EXE", ".BAT", ".CMD" };
  22. char *pathBuffer, *pathString;
  23.  
  24. int main ( int argc, char *argv[] ) {
  25.  
  26.     int i, result = 0;
  27.  
  28.     if ( argc == 1 ) {
  29.         printf ( "%s %s\nUsage:\t%s\n%s\n%s\n%s\n",
  30.             *argv, "$Revision: 1.2 $", USAGE, USAGE1, USAGE2, USAGE3 );
  31.         return 0;
  32.     }
  33.  
  34.     pathBuffer = LocalAlloc ( LMEM_FIXED | LMEM_ZEROINIT, buffSize );
  35.     if ( !pathBuffer ) {
  36.         fprintf ( stderr, "%s\n", "Couldn't allocate memory." );
  37.         return 1;
  38.     }
  39.  
  40.     pathString = LocalAlloc ( LMEM_FIXED | LMEM_ZEROINIT, pathSize );
  41.     if ( !pathString ) {
  42.         fprintf ( stderr, "%s\n", "Couldn't allocate memory." );
  43.         LocalFree ( pathBuffer );
  44.         return 2;
  45.     }
  46.  
  47.     for ( i = 1; i < argc; i++ ) {
  48.  
  49.         if (    !( strcmp ( argv[i], "/?" ) ) ||
  50.             !( strcmp ( argv[i], "/H" ) ) ||
  51.             !( strcmp ( argv[i], "/h" ) ) ||
  52.             !( strcmp ( argv[i], "-H" ) ) ||
  53.             !( strcmp ( argv[i], "-h" ) ) ) {
  54.  
  55.             printf ( "Usage: %s %s\n%s\n%s\n%s\n",
  56.                  *argv, USAGE, USAGE1, USAGE2, USAGE3 );
  57.             continue;
  58.         }
  59.  
  60.         if (    !( strcmp ( argv[i], "/L" ) ) ||
  61.             !( strcmp ( argv[i], "/l" ) ) ||
  62.             !( strcmp ( argv[i], "-L" ) ) ||
  63.             !( strcmp ( argv[i], "-l" ) ) ) {
  64.  
  65.             printf ( "%s: %s\n%s\n%s\n", *argv, LICENSE, LICENSE1, LICENSE2 );
  66.             continue;
  67.         }
  68.  
  69.  
  70.         printf ( "%s:\n", argv[i] );
  71.  
  72.         if ( ( strchr ( argv[i], '.' ) ) )
  73.             result |= dotwhich ( argv[i] );
  74.  
  75.         else
  76.             result |= which ( argv[i] );
  77.     }
  78.  
  79.     LocalFree ( pathBuffer );
  80.     LocalFree ( pathString );
  81.     return result;
  82. }
  83.  
  84. /* no dot extension provided by user, check in order of execution */
  85. int which ( char *name ) {
  86.  
  87.     int i, count = 0;
  88.     char *dotAddress, *temp;
  89.     HANDLE finder;
  90.     WIN32_FIND_DATA lpffd;
  91.  
  92.     /* have to do this each time since strtok writes NULLs in the buffer */
  93.     count = GetEnvironmentVariable ( "PATH", pathBuffer, buffSize ) ;
  94.     if ( count > buffSize ) {
  95.         fprintf ( stderr, "%s\n", "Path string > 16K, you must be joking." );
  96.         LocalFree ( pathBuffer );
  97.         LocalFree ( pathString );
  98.         return 3;
  99.     }
  100.  
  101.     /* check current directory first */
  102.     /* leave room for path + "name" + "\" + ".???" + "\0" */
  103.     strncpy ( pathString, ".", MAX ( ( pathSize - ( strlen ( name ) + 6 ) ), 0 ) );
  104.  
  105.     /* append dotless name of sought */
  106.     strcat ( pathString, "\\" );
  107.     strcat ( pathString, name );
  108.  
  109.     /* dotAddress is where we'll start appending executable suffixes */
  110.     dotAddress = pathString + strlen (pathString);
  111.  
  112.     /* loop trying different extensions */
  113.     for ( i = 0; i < 4; i++ ) {
  114.  
  115.         strcpy ( dotAddress, extensions[i] );
  116.  
  117.         /* Do we find it ? */
  118.         if ( INVALID_HANDLE_VALUE !=
  119.             ( finder = FindFirstFile ( pathString, &lpffd ) ) ) {
  120.             /* System files aren't executed in a CMD.EXE exe path search */
  121.             if ( !( lpffd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) ) {
  122.                 /* It's a findable, print its full path */
  123.                 printf ( "\t%s\n", pathString );
  124.                 return 0;    /* if we did, return no error */
  125.             }
  126.             /* In any event, if we got a handle, close it.
  127.             FindClose ( finder );    /* Close find handle */
  128.         }
  129.     }
  130.  
  131.     /* Wasn't in current dir, we'll loop checking path */
  132.     /* Get first path element. */
  133.     temp = strtok ( pathBuffer, ";" ) ;
  134.  
  135.     /* Loop while strtok() returns something */
  136.     /* see above for more comments on functionality in this loop */
  137.     while ( temp ) {
  138.  
  139.         /* leave room for path + "name" + "\" + ".???" + "\0" */
  140.         strncpy ( pathString, temp, pathSize - ( strlen ( name ) + 6 ) );
  141.  
  142.         strcat ( pathString, "\\" );
  143.         strcat ( pathString, name );
  144.  
  145.         dotAddress = pathString + strlen (pathString);
  146.  
  147.         for ( i = 0; i < 4; i++ ) {
  148.  
  149.             strcpy ( dotAddress, extensions[i] );
  150.  
  151.             /* See comments above */
  152.             if ( INVALID_HANDLE_VALUE !=
  153.                 ( finder = FindFirstFile ( pathString, &lpffd ) ) ) {
  154.  
  155.                 if ( !( lpffd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) ) {
  156.                     printf ( "\t%s\n", pathString );
  157.                     return 0;
  158.                 }
  159.  
  160.                 FindClose ( finder );    /* Close find handle */
  161.             }
  162.         }
  163.  
  164.         /* get next element in path in order to try again */
  165.         temp = strtok ( 0, ";" );
  166.     }
  167.  
  168.     return aFileNotFound;
  169. }
  170.  
  171. /* Already has a dot extension */
  172. int dotwhich ( char *name ) {
  173.  
  174.     int count = 0;
  175.     char *temp;
  176.     HANDLE finder;
  177.     WIN32_FIND_DATA lpffd;
  178.  
  179.     /* have to do this each time since strtok writes NULLs in the buffer */
  180.     count = GetEnvironmentVariable ( "PATH", pathBuffer, buffSize ) ;
  181.     if ( count > buffSize ) {
  182.         fprintf ( stderr, "%s\n", "Path string > 16K, you must be joking." );
  183.         LocalFree ( pathBuffer );
  184.         LocalFree ( pathString );
  185.         return 3;
  186.     }
  187.  
  188.     /* check current directory first */
  189.     /* leave room for path + "name" + \0" */
  190.     strncpy ( pathString, ".", MAX ( ( pathSize - ( strlen ( name ) + 1 ) ), 0 ) );
  191.  
  192.     strcat ( pathString, "\\" );
  193.     strcat ( pathString, name );
  194.  
  195.     /* See comments in which(), above */
  196.     if ( INVALID_HANDLE_VALUE != ( finder = FindFirstFile ( pathString, &lpffd ) ) ) {
  197.  
  198.         if ( !( lpffd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) ) {
  199.             printf ( "\t%s\n", pathString );
  200.             return 0;
  201.         }
  202.  
  203.         FindClose ( finder );    /* Close find handle */
  204.     }
  205.  
  206.     /* Not in current dir, so get first element in path */
  207.     temp = strtok ( pathBuffer, ";" );
  208.  
  209.     while ( temp ) {
  210.  
  211.     /* leave room for path + "name" + \0" */
  212.     strncpy ( pathString, temp, MAX ( ( pathSize - ( strlen ( name ) + 1 ) ), 0 ) );
  213.  
  214.         strcat ( pathString, "\\" );
  215.         strcat ( pathString, name );
  216.  
  217.         if ( INVALID_HANDLE_VALUE != ( finder = FindFirstFile ( pathString, &lpffd ) ) ) {
  218.  
  219.             if ( !( lpffd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) ) {
  220.                 printf ( "\t%s\n", pathString );
  221.                 return 0;
  222.             }
  223.  
  224.             FindClose ( finder );    /* Close find handle */
  225.         }
  226.  
  227.         /* get next element in path in order to try again */
  228.         temp = strtok ( 0, ";" );
  229.     }
  230.  
  231.     return aFileNotFound;
  232. }
  233.  
  234. /* End of which.c */
  235.